package cn.zhp.mapper;

import cn.zhp.entity.SysResource;
import org.apache.ibatis.annotations.CacheNamespace;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;
import tk.mybatis.mapper.common.Mapper;

import java.util.List;

/**
 * mybatis二级缓存注解实现：
 * 1.需要缓存的对象类型实现序列化
 * 2.全局缓存支持 默认开启
 * 3.在需要缓存的mapper上添加注解  @CacheNameSpace
 * blocking:设置是否使用锁进行阻塞   通过装饰者模式，提供一个BlockingCache类来处理缓存
 * 4.mapper的接口方法中对不需要缓存的接口添加@Options(useCache=false)
 */
@CacheNamespace(blocking=true)
public interface SysResourceMapper extends Mapper<SysResource> {

    //根据角色id查询权限信息
    @Select("select  " +
            " sre.* " +
            "from " +
            " sys_role_resource srr,sys_resource sre " +
            "where " +
            " srr.role_id=#{rid} " +
            "and " +
            " sre.del_flag=0 " +
            "and " +
            " srr.resource_id=sre.id")
    @Options(useCache = false)
    List<SysResource> selectByRid(long rid);


    @Select("SELECT DISTINCT " +
            " sre.id, " +
            " sre.`name`, " +
            " sre.common, " +
            " sre.icon, " +
            " sre.sort, " +
            " sre.parent_id, " +
            " sre.type, " +
            " sre.url, " +
            " sre.description, " +
            " sre.`status`, " +
            " sre.parent_ids, " +
            " sre.create_date, " +
            " sre.update_date, " +
            " sre.create_by, " +
            " sre.update_by, " +
            " sre.del_flag, " +
            " sre.permission_str  " +
            "FROM " +
            " sys_user_role sur, " +
            " sys_role sro, " +
            " sys_role_resource srr, " +
            " sys_resource sre  " +
            "WHERE " +
            " sur.user_id = #{uid}  " +
            " AND sre.type=0 " +
            " AND sre.del_flag=0 " +
            " AND sro.del_flag=0 " +
            " AND sur.role_id = sro.id  " +
            " AND sro.id = srr.role_id  " +
            " AND srr.resource_id = sre.id")
    List<SysResource> selectByUid(long uid);


    @Select("select * from sys_resource where type=0  and url <> ''")
    List<SysResource> selectResources();
}